#!c:/perl/bin/perl.exe

use lib qw(../cgi-bin);

use DBI;
use DBD::mysql;
use DIVA_DB;
use Diva::DB;
use WWCOM;
use CGI;
use WiganRL::DataObject;
use WiganRL::DataObject::Teamplayer;
use strict;
    
my $DEBUG= 0;
$DIVA_DB::DEBUG= $DEBUG;

$DIVA_DB::DATABASE_NAME= "wwcom";
$DIVA_DB::DATABASE_SERVER= "wigan.smb.man.ac.uk:3306";

my $cgi= new CGI;
my %outvars;
$cgi->param('UserID',12032);

my $dbh= Diva::DB->new();
$dbh->set( {
    DATABASE_NAME       => 'wwcom',
    DATABASE_SERVER     => 'wigan.smb.man.ac.uk:3306',
    DATABASE_USERNAME   => 'wwcom',
    DATABASE_PASSWORD   => 'wwcom',
    DATABASE_TYPE       => 'mysql',
    } );
$dbh->connect();
$dbh->debug(0);

WiganRL::DataObject->dbh($dbh);
WiganRL::DataObject->cgi($cgi);
WiganRL::Search->dbh($dbh);
#WiganRL::DataObject::User->dbh($dbh);
#WiganRL::DataObject->cachedir("$SITE_BASE_DIR/cached");

$dbh= &DIVA_DB::DB_CONNECT('wwcom','wwcom');

my $seasonname= '1999';

#get season details
my $season_id= '';
my ($sth,$rv)= &DIVA_DB::GET_DATA($dbh,$cgi,\%outvars,['SEASON','OBJECT'],['*'],'',
    "SEASON_NAME='$seasonname' and PK_OBJECT_ID=FK_OBJECT_ID");
if ($rv<=0) {
    print "No season found - breaking\n";
    $sth->finish;
    exit(0);
}
my $reshash= $sth->fetchrow_hashref;
$season_id= $reshash->{FK_OBJECT_ID};
$sth->finish;
print "Got Season: $season_id ($seasonname)\n\n";

#get competition details
my ($sth,$rv)= &DIVA_DB::GET_DATA($dbh,$cgi,\%outvars,['COMPETITION','OBJECT'],['*'],'',
    "FK_PARENT_ID='$season_id' and PK_OBJECT_ID=FK_OBJECT_ID");
if ($rv<=0) {
    print "No competitions found - breaking\n";
    $sth->finish;
    exit(0);
}

while ($reshash= $sth->fetchrow_hashref) {
    my $competition_id= $reshash->{FK_OBJECT_ID};
    print "Got Competition: $competition_id ($reshash->{COMPETITION_NAME})\n\n";

    #get matches
    my $match_id= '';
    my ($sth,$rv)= &DIVA_DB::GET_DATA($dbh,$cgi,\%outvars,['MATCHSTATS','OBJECT'],['*'],'',
        "FK_PARENT_ID='$competition_id' and PK_OBJECT_ID=FK_OBJECT_ID");
    while (my $reshash= $sth->fetchrow_hashref) {
        $match_id= $reshash->{FK_OBJECT_ID};
        print "Processing Match: $match_id ($reshash->{MATCH_NAME})\n";
        print "\t1. Continue\n\t2. Skip\n\t3. Exit\n\t\tChoose Option: ";
        my $choice= <STDIN>;
        if ($choice == 1) {
            update_match($match_id);
        } elsif ($choice == 2) {
            print "\n\nSkipping\n";
            next;
        } else {
            print "\n\nBreaking\n";
            exit(0);
        }
    }
}
$sth->finish;
exit(0);

sub update_match {
    my ($match_id)= @_;

    #get match details
    my ($sth,$rv)= &DIVA_DB::GET_DATA($dbh,$cgi,\%outvars,['MATCHSTATS','OBJECT'],['*'],'',
        "FK_OBJECT_ID='$match_id' and PK_OBJECT_ID=FK_OBJECT_ID");
    if ($rv<=0) {
        print "No match found - breaking\n";
        $sth->finish;
        return;
    }
    my $reshash= $sth->fetchrow_hashref;
    my $kotime= $reshash->{MATCH_KOTIME};
    my $competition_id= $reshash->{FK_PARENT_ID};
    my $homesquad_id= $reshash->{FK_HOMESQUAD_ID};
    my $awaysquad_id= $reshash->{FK_AWAYSQUAD_ID};
    my $home_id= $reshash->{FK_HOME_ID};
    my $away_id= $reshash->{FK_AWAY_ID};
    my $wigansquad_id= $homesquad_id;
    my $othersquad_id= $awaysquad_id;
    my $date= $reshash->{MATCHKOTIME};
    if ($home_id != 10043) {
        $wigansquad_id= $awaysquad_id;
        $othersquad_id= $homesquad_id;
    }
    $sth->finish;
    
    print "Got KOTime: $kotime\n";
    $kotime=~ /^(\d+)\-(\d+)\-(\d+)/;
    my $matchname= "$1$2$3";
    
    #get competition id
    my ($sth,$rv)= &DIVA_DB::GET_DATA($dbh,$cgi,\%outvars,['COMPETITION','OBJECT'],['*'],'',
        "FK_OBJECT_ID='$competition_id' and PK_OBJECT_ID=FK_OBJECT_ID");
    if ($rv<=0) {
        print "No competition found - breaking\n";
        $sth->finish;
        exit(0);
    }
    my $reshash= $sth->fetchrow_hashref;
    my $season_id= $reshash->{FK_PARENT_ID};
    $sth->finish;
    
    #get season name
    my ($sth,$rv)= &DIVA_DB::GET_DATA($dbh,$cgi,\%outvars,['SEASON'],['*'],'',"FK_OBJECT_ID='$season_id'");
    if ($rv<=0) {
        print "No season found - breaking\n";
        $sth->finish;
        exit(0);
    }
    my $reshash= $sth->fetchrow_hashref;
    my $season= $reshash->{SEASON_NAME};
    $sth->finish;
    
    my $file= "$WWCOM::SITEBASEDIR/reports/$season/$matchname.html";
    print "Match file: $file\n";
    if (!-e $file) {
        print "No match file found - breaking\n";
        return;
    }
    open FH,"<$file";
    my @match= <FH>;
    close FH;
    my $match= join('',@match);
    
    #get attendence
    my $attendence= '';
    if ($match=~ /Attendance: ([\d\,]+)/) {
        $attendence= $1;
        $attendence=~ s/\,//eg;
    }
    print "Attendence: $attendence\n";
    #get half time
    my ($homeht,$awayht)= ('','');
    if ($match=~ /Half-time: (\d+)[\,\s\-]+(\d+)/) {
        $homeht= $1;
        $awayht= $2;
    }
    print "HomeHT: $homeht, AwayHT: $awayht\n";
    #get referee
    my ($referee)= ('');
    if ($match=~ /Referee: ([\w\-\s]+)/) {
        $referee= $1;
        chomp $referee;
    }
    print "Referee: $referee\n";
    #get referee id
    my ($sth,$rv)= &DIVA_DB::GET_DATA($dbh,$cgi,\%outvars,['REFEREE'],['*'],'',
        "REFEREE_NAME='$referee'");
    my $referee_id= 0;
    if ($rv<=0) {
        print "No referee found - inserting\n";
        &DIVA_DB::INSERT_DATA($dbh,$cgi,\%outvars,'OBJECT',['FK_CONTENTTYPE_ID','OBJECT_DATE','FK_USER_ID'],
            [[29,'NOW()',12032]]);
        $referee_id= $dbh->{'mysql_insertid'};
        &DIVA_DB::INSERT_DATA($dbh,$cgi,\%outvars,'REFEREE',['FK_OBJECT_ID','REFEREE_NAME'],
            [[$dbh->{'mysql_insertid'},$referee]]);
        print "\tReferee Inserted!\n\n";        
    } else {
        my $reshash= $sth->fetchrow_hashref;
        $referee_id= $reshash->{FK_OBJECT_ID};
    }
    $sth->finish;
    
    
    #update match 
    &DIVA_DB::UPDATE_DATA($dbh,$cgi,\%outvars,'MATCHSTATS',['FK_REFEREE_ID','MATCH_REFEREE',
        'MATCH_HOMEHTSCORE','MATCH_AWAYHTSCORE','MATCH_ATTENDANCE'],
        [$referee_id,$referee,$homeht,$awayht,$attendence],
        "FK_OBJECT_ID='$match_id'");
    print "Updated Match: $match_id!\n";
    
    #wigan team
    #get wigan team
    my ($sth,$rv)= &DIVA_DB::GET_DATA($dbh,$cgi,\%outvars,['TEAM'],['*'],'',
        "FK_MATCHSTATS_ID='$match_id' and FK_CLUB_ID='10043'");
    if ($rv<=0) {
        print "No wigan team found - breaking\n";
        $sth->finish;
        exit(0);
    }
    my $reshash= $sth->fetchrow_hashref;
    my $team_id= $reshash->{FK_OBJECT_ID};
    $sth->finish;

#post 19990509
    update_team($match,$match_id,qr~<\!--\s+PRP\s+\d+\s+-->\s*
        (?:(\d+)\.\s+)?(?:<a[^>]+>)?\s*
        ([\w\s\']+)(?:</a>)?\s*
        (?:(?:\,|\;)\s+<em>([^\n\r\l]+?)</em>)?\s*
        <\!--\s+/PRP\s+\d+\s+-->~six,$team_id,10043,$wigansquad_id,
        qr~<tr>\s*
        <td[^>]+\scolspan=2>\s*
        Coach:\s([\w\s\']+)</td>~sx,$date); #wigan team
#pre 19990509
#    update_team($match,$match_id,qr~(?:(\d+)\.\s+)?(?:<a\s+href="javascript:getPlayer\('[\w\.]+'\)">)?\s*
#        ([\w\s\']+)(?:</a>)?\s*
#        (?:(?:\,|\;)\s+<em>([^\n\r\l]+?)</em>)?\s*</td><td[^>]*>
#        <\!--\s+PR\s+\d+\s+--></td>~six,$team_id,10043,$wigansquad_id,
#        qr~<tr>\s*
#        <td[^>]+\scolspan=2>\s*
#        Coach:\s([\w\s\']+)</td>~sx,$date); #wigan team
    
    #other team
    #get other team
    my ($sth,$rv)= &DIVA_DB::GET_DATA($dbh,$cgi,\%outvars,['TEAM'],['*'],'',
        "FK_MATCHSTATS_ID='$match_id' and FK_CLUB_ID!='10043'");
    if ($rv<=0) {
        print "No other team found - breaking\n";
        $sth->finish;
        exit(0);
    }
    my $reshash= $sth->fetchrow_hashref;
    $team_id= $reshash->{FK_OBJECT_ID};
    my $club_id= $reshash->{FK_CLUB_ID};
    $sth->finish;

#post 19990509
    update_team($match,$match_id,qr~<\!--\s+PR\s+\d+\s+--></td>\s*
        <td[^>]*>\s*(?:(\d+)\.\s+)?(?:<a\s+href="javascript:getPlayer\('[\w\.]+'\)">)?\s*
        ([\w\s\']+)(?:</a>)?\s*
        (?:(?:\,|\;)\s+<em>([^\r\n\l]+?)</em>)?\s*
        </td>~six,$team_id,$club_id,$othersquad_id,
        qr~</td>\s*<td[^>]+>\s*
            Coach:\s([\w\s\']+)\s*</td>~sx,$date); #other team
#pre 19990509
#    update_team($match,$match_id,qr~<\!--\s+PR\s+\d+\s+--></td>\s*
#        <td[^>]*>\s*(?:(\d+)\.\s+)?(?:<a\s+href="javascript:getPlayer\('[\w\.]+'\)">)?\s*
#        ([\w\s\']+)(?:</a>)?\s*
#        (?:(?:\,|\;)\s+<em>([^\r\l\n]+?)</em>)?\s*
#        </td>~six,$team_id,$club_id,$othersquad_id,
#        qr~</td>\s*<td[^>]+>\s*
#            Coach:\s([\w\s\']+)\s*</td>~sx,$date); #other team
}

sub update_team {
    my ($match,$match_id,$pattern,$team_id,$club_id,$squad_id,$coachpattern,$date)= @_;

    #delete team players and scores
    my $reshash;
    my ($sth,$rv)= &DIVA_DB::GET_DATA($dbh,$cgi,\%outvars,['TEAMPLAYER,OBJECT'],['*'],'',
        "FK_PARENT_ID='$team_id' and FK_OBJECT_ID=PK_OBJECT_ID");
    while ($reshash= $sth->fetchrow_hashref) {
        my $tp_id= $reshash->{FK_OBJECT_ID};
        $dbh->do("delete from OBJECT where PK_OBJECT_ID='$tp_id'");
        $dbh->do("delete from TEAMPLAYER where FK_OBJECT_ID='$tp_id'");
        $dbh->do("delete from TEAMPLAYERSCORE where FK_TEAMPLAYER_ID='$tp_id'");
    }
    $sth->finish;
    
    my %players= ();
    my %scores= ();
    my $pos= 1;
    while ($match=~ /$pattern/g) {
        my $squadno= $1;
        my $playername= $2;
        my $playerscore= $3;
        $playerscore=~ s/<.*?>//seg;
        print "Player: $pos, $squadno, $playername, $playerscore\n";
        my $player_firstname= '';
        my $player_lastname= $playername;
        my $where= "PLAYER_LASTNAME=".$dbh->quote($playername);
        my @pn= split(/\s+/,$playername);
        if ($#pn>= 1) {
            $player_firstname= join(' ',@pn[0..($#pn-1)]);
            $player_lastname= $pn[$#pn];
            $where= "PLAYER_LASTNAME=".$dbh->quote($player_lastname).
                " and PLAYER_FIRSTNAME like ".$dbh->quote("\%$player_firstname\%");
            print "$where\n";
        }
        
        # First check if player exists
        my ($sth,$rv)= &DIVA_DB::GET_DATA($dbh,$cgi,\%outvars,['PLAYER'],['*'],'',$where);
        my $player_id;
        if ($rv<=0) {
            my $linkable= ($club_id == 10043) ? 'True' : 'False';
            print "No player found - inserting\n";
            &DIVA_DB::INSERT_DATA($dbh,$cgi,\%outvars,'OBJECT',['FK_CONTENTTYPE_ID','OBJECT_DATE','FK_USER_ID',
                'OBJECT_LINKABLE'],
                [[11,'NOW()',12032,$linkable]]);
            $player_id= $dbh->{'mysql_insertid'};
            &DIVA_DB::INSERT_DATA($dbh,$cgi,\%outvars,'PLAYER',['FK_OBJECT_ID','PLAYER_FULLNAME',
                'PLAYER_FIRSTNAME','PLAYER_LASTNAME','PLAYER_NAME','FK_CLUB_ID'],
                [[$dbh->{'mysql_insertid'},$playername,$player_firstname,$player_lastname,$playername,$club_id]]);
            print "\tPlayer Inserted!\n\n";
        } elsif ($rv==1) {
            my $reshash= $sth->fetchrow_hashref;
            $player_id= $reshash->{FK_OBJECT_ID};
        } else {
            print "Player Clash:\n";
            my $count= 1;
            my @pids= ();
            while (my $reshash= $sth->fetchrow_hashref) {
                print "\t$count.  $reshash->{FK_OBJECT_ID}: $reshash->{PLAYER_FULLNAME}\n";
                $pids[$count]= $reshash->{FK_OBJECT_ID};
                $count++;
            }
            print "\tChoose player:   ";
            my $i= <STDIN>;
            exit(0) if ($i==0);
            $player_id= $pids[$i];
        }
        $sth->finish;
    
        #does this player have a signing to this club?
        my ($sth,$rv)= &DIVA_DB::GET_DATA($dbh,$cgi,\%outvars,['PLAYERSIGNING','OBJECT'],['*'],'',
            "FK_OBJECT_ID=PK_OBJECT_ID and FK_PARENT_ID=$player_id and FK_CLUB_ID=$club_id");
        if ($rv<=0) {
            print "No Player signing to this club - inserting\n";
            &DIVA_DB::INSERT_DATA($dbh,$cgi,\%outvars,'OBJECT',['FK_CONTENTTYPE_ID','OBJECT_DATE','FK_USER_ID','FK_PARENT_ID'],
                [[12,'NOW()',12032,$player_id]]);
            &DIVA_DB::INSERT_DATA($dbh,$cgi,\%outvars,'PLAYERSIGNING',['FK_OBJECT_ID','FK_CLUB_ID',
                'PLAYERSIGNING_STATUS','PLAYERSIGNING_NOTES','PLAYERSIGNING_DATE'],
                [[$dbh->{'mysql_insertid'},$club_id,'Player',
                "Automatically added from match: $match_id\nPlease update details as appropriate",
                $date]]);
            print "\tPlayer Signing Inserted!\n\n";
            
        }
        $sth->finish;
        
        #find squad player
        my $squadplayer_id= '';
        my ($sth,$rv)= &DIVA_DB::GET_DATA($dbh,$cgi,\%outvars,['SQUADPLAYER','OBJECT'],['*'],'',
            "FK_OBJECT_ID=PK_OBJECT_ID and FK_PARENT_ID=$squad_id and FK_PLAYER_ID=$player_id");
        if ($rv<=0) {
            print "No Squad member for this player - inserting\n";
            &DIVA_DB::INSERT_DATA($dbh,$cgi,\%outvars,'OBJECT',['FK_CONTENTTYPE_ID','OBJECT_DATE','FK_USER_ID','FK_PARENT_ID'],
                [[13,'NOW()',12032,$squad_id]]);
            $squadplayer_id= $dbh->{'mysql_insertid'};
            &DIVA_DB::INSERT_DATA($dbh,$cgi,\%outvars,'SQUADPLAYER',['FK_OBJECT_ID','FK_PLAYER_ID',
                'SQUADPLAYER_TYPE','SQUAD_NUMBER'],
                [[$dbh->{'mysql_insertid'},$player_id,'Player',$squadno]]);
            print "\tSquad Player Inserted!\n\n";
            
        } else {
            my $reshash= $sth->fetchrow_hashref;
            $squadplayer_id= $reshash->{FK_OBJECT_ID};        
        }
        $sth->finish;
    
    
        #OK now add a team player and scores
        my $tp= WiganRL::DataObject::Teamplayer->new();
        my $sub= 'False';
        $sub= 'True' if ($pos>=14);
        $cgi->param('ParentID',$team_id);
        $cgi->param('FK_PLAYER_ID',$player_id);
        $cgi->param('FK_SQUADPLAYER_ID',$squadplayer_id);
        $cgi->param('TEAMPLAYER_POSITION',$pos);
        $cgi->param('TEAMPLAYER_SUBSTITUTE',$sub);
        $cgi->param('TEAMPLAYER_MOTMVOTES',0);
        $cgi->param('TEAMPLAYER_SCORES',defined $playerscore ? $playerscore : '');
        print "Inserting team player\n";
        $tp->update;
        print "\tTeam Player Inserted!\n\n";
    
        $players{$playername}= $tp->id;  #remember for substitutes        
        $scores{$pos}{SSTR}= $playerscore;
        $scores{$pos}{TPID}= $tp->id;
        $pos++;
    }
    
    #subs
    for (keys %scores) {
        my $sstr= $scores{$_}{SSTR};
        if ($sstr=~ /replaced\s*
            (?:<a[^>]+>)?([\w\s\']+?)(?:<\/a>)?\s+after\s+(\d+)\s+minutes\s*(?:\(([^\)]+)\))?/sx) {
            my $playername= $1;
            my $time= $2;
            my $notes= $3;
            print "Found substitution: $playername\n";
            if (defined $players{$playername}) {
                print "Found substitute - inserting\n";
                &DIVA_DB::INSERT_DATA($dbh,$cgi,\%outvars,'OBJECT',['FK_CONTENTTYPE_ID','OBJECT_DATE','FK_USER_ID',
                    'FK_PARENT_ID'],
                    [[16,'NOW()',12032,$scores{$_}{TPID}]]);
                my $tps_id= $dbh->{'mysql_insertid'};
                &DIVA_DB::INSERT_DATA($dbh,$cgi,\%outvars,'TEAMPLAYERSCORE',['FK_OBJECT_ID','FK_SCORE_ID',
                    'FK_TEAMPLAYER_ID','TEAMPLAYERSCORE_TIME','TEAMPLAYERSCORE_COMMENT'],
                    [[$dbh->{'mysql_insertid'},11961,$players{$playername},$time,$notes]]);
                print "\tSubstitution Inserted!\n\n";
            }            
        }
    }
    
    #coaches
    if ($match=~ $coachpattern) {
        my $playername= $1;
        
        # First check if player exists
        my $player_firstname= '';
        my $player_lastname= $playername;
        my $where= "PLAYER_LASTNAME=".$dbh->quote($playername);
        my @pn= split(/\s+/,$playername);
        if ($#pn>= 1) {
            $player_firstname= join(' ',@pn[0..($#pn-1)]);
            $player_lastname= $pn[$#pn];
            $where= "PLAYER_LASTNAME=".$dbh->quote($player_lastname).
                " and PLAYER_FIRSTNAME like ".$dbh->quote("\%$player_firstname\%");
            print "$where\n";
        }
        my ($sth,$rv)= &DIVA_DB::GET_DATA($dbh,$cgi,\%outvars,['PLAYER'],['*'],'',$where);
        my $player_id;
        if ($rv<=0) {
            my $linkable= ($club_id == 10043) ? 'True' : 'False';
            print "No player found - inserting\n";
            &DIVA_DB::INSERT_DATA($dbh,$cgi,\%outvars,'OBJECT',['FK_CONTENTTYPE_ID','OBJECT_DATE','FK_USER_ID',
                'OBJECT_LINKABLE'],
                [[11,'NOW()',12032,$linkable]]);
            $player_id= $dbh->{'mysql_insertid'};
            &DIVA_DB::INSERT_DATA($dbh,$cgi,\%outvars,'PLAYER',['FK_OBJECT_ID','PLAYER_FULLNAME',
                'PLAYER_FIRSTNAME','PLAYER_LASTNAME','PLAYER_NAME','FK_CLUB_ID'],
                [[$dbh->{'mysql_insertid'},$playername,$player_firstname,$player_lastname,$playername,$club_id]]);
            print "\tPlayer Inserted!\n\n";
        } elsif ($rv==1) {
            my $reshash= $sth->fetchrow_hashref;
            $player_id= $reshash->{FK_OBJECT_ID};
        } else {
            print "Player Clash:\n";
            my $count= 1;
            my @pids= ();
            while (my $reshash= $sth->fetchrow_hashref) {
                print "\t$count.  $reshash->{FK_OBJECT_ID}: $reshash->{PLAYER_FULLNAME}\n";
                $pids[$count]= $reshash->{FK_OBJECT_ID};
                $count++;
            }
            print "\tChoose player:   ";
            my $i= <STDIN>;
            exit(0) if ($i==0);
            $player_id= $pids[$i];
        }
        $sth->finish;
    
        #does this player have a signing to this club?
        my $signing_id= '';
        my ($sth,$rv)= &DIVA_DB::GET_DATA($dbh,$cgi,\%outvars,['PLAYERSIGNING','OBJECT'],['*'],'',
            "FK_OBJECT_ID=PK_OBJECT_ID and FK_PARENT_ID=$player_id and FK_CLUB_ID=$club_id");
        if ($rv<=0) {
            print "No Coach signing to this club - inserting\n";
            &DIVA_DB::INSERT_DATA($dbh,$cgi,\%outvars,'OBJECT',['FK_CONTENTTYPE_ID','OBJECT_DATE','FK_USER_ID','FK_PARENT_ID'],
                [[12,'NOW()',12032,$player_id]]);
            $signing_id= $dbh->{'mysql_insertid'};
            &DIVA_DB::INSERT_DATA($dbh,$cgi,\%outvars,'PLAYERSIGNING',['FK_OBJECT_ID','FK_CLUB_ID',
                'PLAYERSIGNING_STATUS','PLAYERSIGNING_NOTES','FK_STAFFTYPE_ID','PLAYERSIGNING_DATE',
                'PLAYERSIGNING_LEAVEDATE'],
                [[$dbh->{'mysql_insertid'},$club_id,'Coach',
                "Automatically added from match: $match_id\nPlease update details as appropriate",
                18398,'0000-00-00','0000-00-00']]);
            print "\tCoach Signing Inserted!\n\n";
            
        } else {
            my $reshash= $sth->fetchrow_hashref;
            $signing_id= $reshash->{FK_OBJECT_ID};
            print "\tCoach Signing found: $signing_id\n\n";
        }
        $sth->finish;

        #find squad staff
        my $squadstaff_id= '';
        my ($sth,$rv)= &DIVA_DB::GET_DATA($dbh,$cgi,\%outvars,['SQUADSTAFF','OBJECT'],['*'],'',
            "FK_OBJECT_ID=PK_OBJECT_ID and FK_SQUAD_ID=$squad_id and FK_PLAYERSIGNING_ID=$signing_id");
        if ($rv<=0) {
            print "No Squad staff for this coach - inserting\n";
            &DIVA_DB::INSERT_DATA($dbh,$cgi,\%outvars,'OBJECT',['FK_CONTENTTYPE_ID','OBJECT_DATE','FK_USER_ID','FK_PARENT_ID'],
                [[13,'NOW()',12032,$player_id]]);
            $squadstaff_id= $dbh->{'mysql_insertid'};
            &DIVA_DB::INSERT_DATA($dbh,$cgi,\%outvars,'SQUADSTAFF',['FK_OBJECT_ID','FK_PLAYERSIGNING_ID',
                'FK_SQUAD_ID'],
                [[$dbh->{'mysql_insertid'},$signing_id,$squad_id]]);
            print "\tSquad Staff Inserted!\n\n";
            
        } else {
            my $reshash= $sth->fetchrow_hashref;
            $squadstaff_id= $reshash->{FK_OBJECT_ID};        
        }
        $sth->finish;

    }
}

